<?php
/********************************************************************
Product    : Plotalot
Date       : 7 August 2011
Copyright  : Les Arbres Design 2010-2011
Contact    : http://extensions.lesarbresdesign.info
Licence    : GNU General Public License
*********************************************************************/

defined('_JEXEC') or die('Restricted Access');

jimport('joomla.application.component.view');

require_once JPATH_COMPONENT.DS.'helpers'.DS.'view_helper.php';

class PlotalotViewChart extends JView
{
function display($tpl = null)
{

// the controller has already populated the data into $this->chart_data

	if ($this->chart_data->id < 1)					// creating a new record
		JToolBarHelper::title(LA_COMPONENT_NAME.': <small><small>'.JText::_('COM_PLOTALOT_NEW_CHART').'</small></small>', 'plotalot.png');
	else
		JToolBarHelper::title(LA_COMPONENT_NAME.': <small><small>'.JText::_('COM_PLOTALOT_EDIT_CHART').' '.$this->chart_data->id.'</small></small>', 'plotalot.png' );

	JToolBarHelper::apply();
	JToolBarHelper::save();
	JToolBarHelper::cancel();

// get component parameters
	
	$params = JComponentHelper::getParams(LA_COMPONENT);
	$trace = $params->get('trace',0);
	$tooltips = $params->get('tooltips',1);

	if ($tooltips)
		{
		$chart_name_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_CHART_NAME').'"';
		$x_size_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_X_SIZE').'"';
		$y_size_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_Y_SIZE').'"';
		$title_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_TITLE').'"';
		$colour_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_COLOUR').'"';
		$grid_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_GRID').'"';
		$html_style_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_HTML_STYLE').'"';
		$db_name_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_DB_NAME').'"';
		$db_host_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_DB_HOST').'"';
		$db_user_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_DB_USER').'"';
		$db_pass_tooltip = $db_user_tooltip;
		$extra_parms_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_EXTRA_PARAMS').'"';
		$num_plots_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_NUM_PLOTS').'"';
		$show_raw_data_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_SHOW_RAW').'"';
		$x_title_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_X_TITLE').'"';
		$x_start_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_X_START').'"';
		$x_end_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_X_END').'"';
		$x_format_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_X_FORMAT').'"';
		$x_num_labels_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_X_NUM_LABELS').'"';
		$y_title_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_Y_TITLE').'"';
		$y_start_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_Y_START').'"';
		$y_end_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_Y_END').'"';
		$y_format_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_Y_FORMAT').'"';
		$y_num_labels_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_Y_NUM_LABELS').'"';
		$plot_name_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_PLOT_NAME').'"';
		$plot_style_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_PLOT_STYLE').'"';
		$plot_enable_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_PLOT_ENABLE').'"';
		$plot_query_tooltip = 'title = "'.JText::_('COM_PLOTALOT_TOOLTIP_PLOT_QUERY').'"';
		}
	else
		{
		$chart_name_tooltip = '';
		$x_size_tooltip = '';
		$y_size_tooltip = '';
		$title_tooltip = '';
		$colour_tooltip = '';
		$grid_tooltip = '';
		$html_style_tooltip = '';
		$db_name_tooltip = '';
		$db_host_tooltip = '';
		$db_user_tooltip = '';
		$db_pass_tooltip = '';
		$extra_parms_tooltip = '';
		$num_plots_tooltip = '';
		$show_raw_data_tooltip = '';
		$x_title_tooltip = '';
		$x_start_tooltip = '';
		$x_end_tooltip = '';
		$x_format_tooltip = '';
		$x_num_labels_tooltip = '';
		$y_title_tooltip = '';
		$y_start_tooltip = '';
		$y_end_tooltip = '';
		$y_format_tooltip = '';
		$y_num_labels_tooltip = '';
		$plot_name_tooltip = '';
		$plot_style_tooltip = '';
		$plot_enable_tooltip = '';
		$plot_query_tooltip = '';
		}
	
	
	if ($params->get('autocomplete',1))
		$autocomplete = 'autocomplete="off"';
	else
		$autocomplete = '';
		
	$plotalot = new Plotalot;

	?>
	<form action="index.php" method="post" name="adminForm" id="adminForm" <?php echo $autocomplete; ?> >

	<input type="hidden" name="option" value="<?php echo LA_COMPONENT; ?>" />
	<input type="hidden" name="id" value="<?php echo $this->chart_data->id; ?>" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="controller" value="" />
	<?php echo JHTML::_('form.token'); ?>

	<table cellspacing="0" width="100%">
	<tr>
	<td width="50%">
		<fieldset class="adminform plotalot_form"><legend><?php echo JText::_('COM_PLOTALOT_CHART'); ?></legend>
		<table cellspacing="0" width="100%">
		<tr>
			<td align="right">
				<?php 
				echo JText::_('COM_PLOTALOT_NAME');
				?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="chart_name" size="80" 
					maxlength="250" '.$chart_name_tooltip.' value="'.htmlspecialchars($this->chart_data->chart_name).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php 
				  echo JText::_('COM_PLOTALOT_TYPE');
				?>
			</td>
			<td>
				<?php 
  				echo LA_view::make_list('chart_type', $this->chart_data->chart_type, $plotalot->chart_types, CHART_TYPE_LINE);
  				echo ' '.JText::_('COM_PLOTALOT_LEGEND');
  				echo LA_view::make_list('legend_type', $this->chart_data->legend_type, $plotalot->legendTypes);
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php 
				echo JText::_('COM_PLOTALOT_SIZE');
				?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="x_size" '.$x_size_tooltip.'  size="8" 
					value="'.$this->chart_data->x_size.'" />';
				echo ' x ';
				echo '<input class="text_area" type="text" name="y_size" '.$y_size_tooltip.'  size="8" 
					value="'.$this->chart_data->y_size.'" />';
				?>
			</td>
		</tr>

		<tr>
			<td align="right">
				<?php 
				echo JText::_('COM_PLOTALOT_TITLE');
				?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="chart_title" size="60" maxlength="2000" '.$title_tooltip.' value="';
				echo htmlspecialchars($this->chart_data->chart_title).'" />';
				echo ' '.JText::_('COM_PLOTALOT_COLOUR').' ';
				echo '<input name="chart_title_colour" class="color {required:false}" size="6" '.$colour_tooltip.' value="'.$this->chart_data->chart_title_colour.'" />';
				?>
			</td>
		</tr>

		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_BACKGROUND'); ?>
			</td>
			<td>
				<?php
				echo LA_view::make_list('back_style', $this->chart_data->back_style, $plotalot->backgroundTypes);
				echo ' '.JText::_('COM_PLOTALOT_COLOURS').' ';
				echo '<input name="back_colour" class="color {required:false}" size="6" '.$colour_tooltip.' value="'.$this->chart_data->back_colour.'" />';
				echo ' - <input name="back_colour_2" class="color {required:false}" size="6" '.$colour_tooltip.' value="'.$this->chart_data->back_colour_2.'" />';
				echo ' '.LA_view::make_checkbox('show_grid', $this->chart_data->show_grid,JText::_('COM_PLOTALOT_GRID'), $grid_tooltip);
				?>
			</td>
		</tr>

		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_HTML_STYLE'); ?>
			</td>
			<td>
				<?php
				echo '<input class="text_area" type="text" name="chart_css_style" size="80" 
				maxlength="255" '.$html_style_tooltip.'  value="'.htmlspecialchars($this->chart_data->chart_css_style).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_DATABASE'); ?>
			</td>
			<td>
				<?php
				echo '<input class="text_area" type="text" name="db_name" size="18" 
					maxlength="250" '.$db_name_tooltip.' value="'.
				htmlspecialchars($this->chart_data->db_name).'" />';
				echo ' '.JText::_('COM_PLOTALOT_HOST');
				echo '<input class="text_area" type="text" name="db_host" size="10" 
					maxlength="250" '.$db_host_tooltip.' value="'.
				htmlspecialchars($this->chart_data->db_host).'" />';
				echo ' '.JText::_('COM_PLOTALOT_USER');
				echo '<input class="text_area" type="text" name="db_user" size="10" 
					maxlength="250" '.$db_user_tooltip.' value="'.
				htmlspecialchars($this->chart_data->db_user).'" />';
				echo ' '.JText::_('COM_PLOTALOT_PASSWORD');
				echo '<input class="text_area" type="password" name="db_pass" size="9" 
					maxlength="250" '.$db_pass_tooltip.' value="'.
				htmlspecialchars($this->chart_data->db_pass).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_EXTRA_PARAMS'); ?>
			</td>
			<td>
				<?php 
				echo '<input type="text" class="text_area" size="80" maxlength="255" name="extra_parms" '.$extra_parms_tooltip.' value = "'.$this->chart_data->extra_parms.'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_NUM_PLOTS'); ?>
			</td>
			<td>
				<?php 
				echo '<input type="text" class="text_area" size="5" name="num_plots" '.$num_plots_tooltip.' value = "'.$this->chart_data->num_plots.'" />';
				echo ' '.LA_view::make_checkbox('show_raw_data', $this->chart_data->show_raw_data,JText::_('COM_PLOTALOT_SHOW_RAW'), $show_raw_data_tooltip);
				?>
			</td>
		</tr>
		</table>
		</fieldset>
	</td>
	<td width="50%">
		<fieldset class="adminform plotalot_form"><legend><?php echo JText::_('COM_PLOTALOT_AXES'); ?></legend>
		<table cellspacing="0" width="100%">
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_X_TITLE'); ?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="x_title" id="x_title" size="80" maxlength="2000" '.$x_title_tooltip.' value="';
				echo htmlspecialchars($this->chart_data->x_title).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_X_START'); ?>
			</td>
			<td>
				<?php
				echo '<input class="text_area" type="text" name="x_start" id="x_start" size="80" maxlength="2000" '.$x_start_tooltip.' value="';
				echo htmlspecialchars($this->chart_data->x_start).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_X_END'); ?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="x_end" id="x_end" size="80" maxlength="2000" '.$x_end_tooltip.' value="';
				echo htmlspecialchars($this->chart_data->x_end).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_X_NUM_LABELS'); ?>
			</td>
			<td>
				<?php
				echo '<input type="text" size="5" name="x_labels" '.$x_num_labels_tooltip.' value = "'.$this->chart_data->x_labels.'" />';
				echo ' '.JText::_('COM_PLOTALOT_X_LABEL_FORMAT').' '.LA_view::make_list('x_format', $this->chart_data->x_format, $plotalot->dataFormats,FORMAT_NONE,' '.$x_format_tooltip.'');
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_Y_TITLE'); ?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="y_title" id="y_title" size="80" maxlength="2000" '.$y_title_tooltip.' value="';
				echo htmlspecialchars($this->chart_data->y_title).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_Y_START'); ?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="y_start" id="y_start" size="80" maxlength="2000" '.$y_start_tooltip.' value="';
				echo htmlspecialchars($this->chart_data->y_start).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_Y_END'); ?>
			</td>
			<td>
				<?php 
				echo '<input class="text_area" type="text" name="y_end" id="y_end" size="80" maxlength="2000" '.$y_end_tooltip.' value="';
				echo htmlspecialchars($this->chart_data->y_end).'" />';
				?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<?php echo JText::_('COM_PLOTALOT_Y_NUM_LABELS'); ?>
			</td>
			<td>
				<?php
				echo '<input type="text" size="5" name="y_labels" '.$y_num_labels_tooltip.' value = "'.$this->chart_data->y_labels.'" />';
				echo ' '.JText::_('COM_PLOTALOT_Y_LABEL_FORMAT').' '.LA_view::make_list('y_format', $this->chart_data->y_format, $plotalot->dataFormats,FORMAT_NONE,' '.$y_format_tooltip.'');
				?>
			</td>
		</tr>
		</table>
		</fieldset>
	</td>
	</tr>
	</table>

	<?php
	for ($i = 0; $i < $this->chart_data->num_plots; $i++)
		{
		$legend_field = 'plot_legend_'.$i;
		$colour_field = 'plot_colour_'.$i;
		$style_field = 'plot_style_'.$i;
		$query_field = 'plot_query_'.$i;
		$enable_field = 'plot_enable_'.$i;
		
		if (isset($this->chart_data->plot_array[$i]['legend']))
			$legend = $this->chart_data->plot_array[$i]['legend'];
		else
			$legend = '';
			
		if (isset($this->chart_data->plot_array[$i]['colour']))
			$colour = $this->chart_data->plot_array[$i]['colour'];
		else
			$colour = '';
			
		if (isset($this->chart_data->plot_array[$i]['style']))
			$style = $this->chart_data->plot_array[$i]['style'];
		else
			$style = 0;
		$style = $plotalot->checkPlotStyle($this->chart_data->chart_type, $style);

		if (isset($this->chart_data->plot_array[$i]['query']))
			$query = $this->chart_data->plot_array[$i]['query'];
		else
			$query = '';

		if (isset($this->chart_data->plot_array[$i]['enable']))
			$enable = $this->chart_data->plot_array[$i]['enable'];
		else
			$enable = 0;
		
		// make sure the plot style makes sense for the chart type
		
		echo "\n".'<div><fieldset class="adminform plotalot_form"><legend>'.JText::_('COM_PLOTALOT_PLOT').' '.($i+1).'</legend>';
		echo '<table cellspacing="0" width="100%"><tr><td align="right">';
		echo '<div>'.JText::_('COM_PLOTALOT_NAME').': ';
		echo '<input type="text" size="25" name="'.$legend_field.'" '.$plot_name_tooltip.' value = "'.$legend.'" />';
		echo '</div><div style="padding-top:3px;">'.JText::_('COM_PLOTALOT_COLOUR').': ';
		echo '<input name="'.$colour_field.'" class="color {required:false}" size="6" '.$colour_tooltip.' value="'.$colour.'" />';
		echo '</div><div style="padding-top:3px;">'.JText::_('COM_PLOTALOT_STYLE').': ';
		echo LA_view::make_list($style_field, $style, $plotalot->lineStyles, LINE_NORMAL, $plot_style_tooltip);
		echo '</div><div style="padding-top:3px;">';
		
		// if the plot is new (no query or name) default it to enabled
		
		if (($legend == '') and ($query == ''))
			$enable = true;
			
		echo LA_view::make_checkbox($enable_field, $enable, JText::_('COM_PLOTALOT_ENABLE'), $plot_enable_tooltip);
		echo '</div></td><td><textarea name="'.$query_field.'" rows="4" cols="100" style="width:auto" '.$plot_query_tooltip.' >'.htmlspecialchars($query).'</textarea></td>';
		echo '</tr></table>';
		echo '</fieldset></div>';
		}
	?>
	<div class="clr"></div>

	</form>
	<?php
	
// if new, don't try to draw the chart
	
	if ($this->chart_data->id == 0)
		return;

// draw the chart			

	$chart_html = $plotalot->drawChart($this->chart_data,$trace);
	if ($plotalot->error != '')
		echo '<div class="plotalot_error">'.JText::_('COM_PLOTALOT_ERROR').': '.$plotalot->error.'</div>';
	if ($plotalot->warning != '')
		echo '<div class="plotalot_error">'.JText::_('COM_PLOTALOT_WARNING').': '.$plotalot->warning.'</div>';
	echo $chart_html;
	
// if show raw data was selected, show each plot as a table of up to 20 rows

	if ($this->chart_data->show_raw_data)
		{
		echo '<br />';
		$this->chart_data->chart_type = CHART_TYPE_TABLE;
		$this->chart_data->chart_css_style = 'border="1" cellspacing="0" cellpadding="2"';
		$this->chart_data->y_size = 20;
		for ($p = 0; $p < $this->chart_data->num_plots; $p++)
			{
			echo '<br />';
			if (empty($this->chart_data->plot_array[$p]['enable']))
				continue;
			if (!$this->chart_data->plot_array[$p]['enable'])			// plot is disabled
				continue;
			if ($this->chart_data->plot_array[$p]['query'] == '')		// no query
				continue;
			$this->chart_data->plot_array[0]['query'] = $this->chart_data->plot_array[$p]['query']; // table query is always plot zero
			$this->chart_data->chart_title = JText::_('COM_PLOTALOT_RAW_DATA').', '.JText::_('COM_PLOTALOT_PLOT').' '.($p + 1)." (".$this->chart_data->plot_array[$p]['legend'].")";
			$chart_html = $plotalot->drawChart($this->chart_data);
			echo $chart_html;
			}
		}
}
}